package com.xushi.service.impl;

import com.xushi.bean.ApplyPriceResult;
import com.xushi.dao.ApplyPriceRepository;
import com.xushi.model.ApplyPrice;
import com.xushi.model.ApplyPricePK;
import com.xushi.model.UserInfo;
import com.xushi.service.ApplyPriceService;
import com.xushi.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class ApplyPriceServiceImpl implements ApplyPriceService {
    @Autowired
    private ApplyPriceRepository applyPriceDao;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public ApplyPriceResult applyPrice(int activityId, String openid) {
        UserInfo userInfo = userInfoService.findUserInfoByOpenId(openid);
        if (ObjectUtils.isEmpty(userInfo)) {
            // 需要输入用户信息
            return new ApplyPriceResult(2, "请填写用户信息");
        }

        ApplyPrice applyPrice = applyPriceDao.findApplyPriceByApplyPricePK(new ApplyPricePK(openid, activityId));

        if (ObjectUtils.isEmpty(applyPrice)) {
            // 未领取过
            applyPriceDao.save(new ApplyPrice(new ApplyPricePK(openid, activityId)));
            return new ApplyPriceResult(1, "申请成功");
        }

        return new ApplyPriceResult(3, "已经领取过");

    }
}
